package disruptor;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;

import java.nio.ByteBuffer;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by dongjigong on 2018/5/25.
 *
 * @Email osbornjonny@126.com
 * @Description
 */
public class ClearingMain {

    public static void translate(ObjectEvent<String> event, long sequence, ByteBuffer buffer) {
        event.setVal(String.valueOf(buffer.getLong(0)) + "----sequence---" + sequence);
    }

    public static void main(String[] args) throws InterruptedException {
        Executor executor = Executors.newCachedThreadPool();
        Disruptor<ObjectEvent<String>> disruptor = new Disruptor<ObjectEvent<String>>(
                () -> new ObjectEvent<String>(), 1024, new CustomThreadFactoryBuilder()
                .setNamePrefix("DemoPool-Thread").setDaemon(true)
                .setPriority(Thread.MAX_PRIORITY).build());

        disruptor
                .handleEventsWith(new ProcessingEventHandler())
                .then(new ClearingObjectHandler());
        disruptor.start();
        RingBuffer<ObjectEvent<String>> ringBuffer = disruptor.getRingBuffer();

        ByteBuffer bb = ByteBuffer.allocate(8);
        for (long l = 0; true; l++) {
            bb.putLong(0, l);
            ringBuffer.publishEvent(ClearingMain::translate, bb);
            //System.out.println(obj.getVal());;
            //Thread.sleep(1000);
        }

    }
}
